package com.agua.enturno.vista;

import com.agua.enturno.vista.paginas.adm.agendas.AgendasPage;
import com.agua.enturno.vista.paginas.home.HomePage;
import com.agua.enturno.vista.paginas.login.LoginPage;
import com.agua.enturno.vista.paginas.perfil.ProfilePage;
import com.agua.enturno.vista.paginas.turnos.TurnosPage;
import com.agua.enturno.vista.util.RolDeUsuario;
import org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;

public class BasePage extends WebPage {

	private static final long serialVersionUID = 1L;
	
	private FeedbackPanel feedbackPanel;

	@SuppressWarnings("serial")
	public BasePage() {
		
		final EnTurnoSession enTurnoSession = (EnTurnoSession) getSession();
		
		feedbackPanel = new FeedbackPanel("feedback");
		feedbackPanel.setOutputMarkupId(true);
		feedbackPanel.setMaxMessages(1);
		add(feedbackPanel);
		
		Link<String> homePageLink = new Link<String>("home-page") {
			public void onClick() {
				setResponsePage(new HomePage());
			}
		};
		add(homePageLink);
		
		Label userName = new Label("user-fullname", new PropertyModel<String>(this, "session.usuario.nombreCompleto"));
		add(userName);
		MetaDataRoleAuthorizationStrategy.authorize(userName, RENDER, RolDeUsuario.CLIENTE);

		Link<String> profilePageLink = new Link<String>("profile-page") {
			public void onClick() {
				setResponsePage(new ProfilePage());
			}
		};
		add(profilePageLink);
		MetaDataRoleAuthorizationStrategy.authorize(profilePageLink, RENDER, RolDeUsuario.CLIENTE);
		
		Link<String> agendasPageLink = new Link<String>("agendas-page") {
			public void onClick() {
				setResponsePage(new AgendasPage());
			}
		};
		add(agendasPageLink);
		MetaDataRoleAuthorizationStrategy.authorize(agendasPageLink, RENDER, RolDeUsuario.ADM_AGENDA);
		
		Link<String> turnosPageLink = new Link<String>("turnos-page") {
			public void onClick() {
				setResponsePage(new TurnosPage());
			}
		};
		add(turnosPageLink);
		MetaDataRoleAuthorizationStrategy.authorize(turnosPageLink, RENDER, RolDeUsuario.CLIENTE);
		
		Link<String> loginPageLink = new Link<String>("login") {
			public void onClick() {
				setResponsePage(new LoginPage());
			}
			public boolean isVisible() {
				return !enTurnoSession.hayUsuarioLogueado() && !BasePage.this.getClass().equals(LoginPage.class);
			}
		};
		add(loginPageLink);
		
		Link<String> logoutPageLink = new Link<String>("logout") {
			public void onClick() {
				
				enTurnoSession.logoutUser();
				setResponsePage(new HomePage());
			}
			public boolean isVisible() {
				return enTurnoSession.hayUsuarioLogueado();
			}
		};
		add(logoutPageLink);
	}
	
	protected EnTurnoSession getEnTurnoSession() {
		return (EnTurnoSession) getSession();
	}
	
	public FeedbackPanel getFeedbackPanel() {
		return feedbackPanel;
	}
	
}
